YUI.add('dao', function(Y, NAME) {

	var db = require('mysql');

	/*
	 * connection.query('SELECT * FROM Presentation', function(err, rows,
	 * fields) { console.log('Executed successfully? ' + (err == null));
	 * console.log('Executed successfully? ' + err); //console.log('Rows: ' +
	 * rows); console.log('Fields: ' + Y.JSON.stringify(Object.keys(fields)));
	 * }); //
	 */

	function handleDisconnect(connection) {
		connection.on('error', function(err) {
			if (!err.fatal) {
				return;
			}

			if (err.code !== 'PROTOCOL_CONNECTION_LOST') {
				throw err;
			}

			console.log('Re-connecting lost connection: ' + err.stack);

			connection = db.createConnection(connection.config);
			handleDisconnect(connection);
			connection.connect();
		});
	}

	// handleDisconnect(connection);

	function DAO() {
		this.connection = db.createClient({
			'host' : 'mysql-server',
			'port' : 3306,
			'user' : 'fmas',
			'password' : 'fmas',
			'database' : 'findmeastore'
		});
		this.space = ' ';
		this.quote = '"';
		handleDisconnect(this.connection);
	}

	Y.extend(DAO, Y.Base, {
		select : function(tableName, callback, limit, offset, params) {
			var query = "Select * from " + tableName;
			if (params && params.conditions
					&& params.conditions instanceof Array) {
				var whereClause = "", whereArr = new Array();
				for ( var i in params.conditions) {
					var param = params.conditions[i];
					Y.log('Processing: ' + Y.JSON.stringify(param), 'debug',
							'dao.server.js');
					var value = param.value;
					var key = param.key;
					var operator = param.op || " = ";
					if ((value && key) || (key && param.unary)) {
						var condition = key + this.space + operator;
						if (value) {
							condition += this.space + this.quote + value
									+ this.quote;
						}
						whereArr.push(condition);
					}
				}
				if (whereArr.length > 0) {
					whereClause = " WHERE " + whereArr.join(" AND ");
					query += whereClause;
				}
			}
			if (params && params.order && params.order instanceof Array) {
				var orderClause = "", orderArr = new Array();
				for ( var i in params.order) {
					var param = params.order[i];
					Y.log('Processing: ' + Y.JSON.stringify(param), 'debug',
							'dao.server.js');
					var key = param.key;
					var operator = param.op || "ASC";
					if (key) {
						orderArr.push(key + this.space + operator);
					}
				}
				if (orderArr.length > 0) {
					orderClause = " ORDER BY " + orderArr.join(" , ");
					query += orderClause;
				}
			}

			if (limit) {
				query += " LIMIT " + limit;
			}
			if (offset) {
				query += " OFFSET " + offset;
			}
			// Y.log('Query: ' + query, 'debug', 'dao.server.js');
			this.connection.query(query, function(err, rows, fields) {
				if (rows) {
					Y.log(query + ' \nReturned ' + rows.length + ' results',
							'debug', 'dao.server.js');
				}
				if (err) {
					Y.log(query + ' \n ' + err, 'error',
							'dao.server.js');
				}
				callback(err, rows, fields);
			});
		},

		queryWithParams : function(queryStr, fields, callback) {
			this.connection.query(queryStr, fields, callback);
		}
	});

	Y.namespace('fmas').DAO = DAO;

}, "0.1.0", {
	"requires" : [ 'json', "base", 'json-stringify' ]
});